package com.clx.compress;

import com.clx.compress.impl.GzipCompressor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class CompressorFactory {
    private final static ConcurrentHashMap<String, CompressWrapper> COMPRESSOR_CACHE = new ConcurrentHashMap<>(8);
    private final static ConcurrentHashMap<Byte,CompressWrapper> COMPRESSOR_CACHE_CODE = new ConcurrentHashMap<>(8);

    static {
        CompressWrapper gzip = new CompressWrapper ((byte) 1,"gzip",new GzipCompressor());
        COMPRESSOR_CACHE.put("gzip",gzip); // 修正：用 "gzip" 作为键存储
        COMPRESSOR_CACHE_CODE.put((byte) 1,gzip);
    }
    // 使用工厂方法获取一个CompressorWrapper
    public static CompressWrapper getCompressor(String compressType) {
        CompressWrapper compressWrapper = COMPRESSOR_CACHE.get(compressType);
        if (compressWrapper == null){
            log.error("为找到您配置的【{}】压缩算法，默认选用gzip算法",compressType);
            return COMPRESSOR_CACHE.get("gzip");
        }
        return compressWrapper;
    }

    public static CompressWrapper getCompressor(byte serializeCode) {
        CompressWrapper compressWrapper = COMPRESSOR_CACHE_CODE.get(serializeCode);
        if (compressWrapper == null){
            log.error("为找到您配置的编号为【{}】压缩算法，默认选用gzip算法",serializeCode);
            return COMPRESSOR_CACHE_CODE.get("gzip");
        }
        return compressWrapper;
    }
}

